package com.iteaj.iboot.plugin.crawler.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iteaj.framework.result.DetailResult;
import com.iteaj.framework.result.Result;
import com.iteaj.iboot.plugin.crawler.entity.SiteContent;
import com.iteaj.iboot.plugin.crawler.entity.SiteContentMedia;
import com.iteaj.iboot.plugin.crawler.mapper.SiteContentMapper;
import com.iteaj.iboot.plugin.crawler.mapper.SiteContentMediaMapper;
import com.iteaj.iboot.plugin.crawler.service.ISiteContentService;
import com.iteaj.framework.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 采集的站点内容 服务实现类
 * </p>
 *
 * @author iteaj
 * @since 2024-10-05
 */
@Service
public class SiteContentServiceImpl extends BaseServiceImpl<SiteContentMapper, SiteContent> implements ISiteContentService {

    private final SiteContentMediaMapper mediaMapper;

    public SiteContentServiceImpl(SiteContentMediaMapper mediaMapper) {
        this.mediaMapper = mediaMapper;
    }

    @Override
    public DetailResult<SiteContent> getByPromotionId(String promotionId) {
        return this.getOne(Wrappers.<SiteContent>lambdaQuery().eq(SiteContent::getContentId, promotionId));
    }

    @Override
    public void saveMedias(List<SiteContentMedia> medias) {
        if(CollectionUtil.isNotEmpty(medias)) {
            medias.forEach(mediaMapper::insert);
        }
    }

    @Override
    public void updateMediaContent(String contentId, String string) {
        this.mediaMapper.update(Wrappers.<SiteContentMedia>lambdaUpdate()
                .eq(SiteContentMedia::getOriMediaId, contentId)
                .set(SiteContentMedia::getContent, string));
    }

    @Override
    public void updateStatus(String promotionId, String status) {
        this.update(Wrappers.<SiteContent>lambdaUpdate()
                .eq(SiteContent::getContentId, promotionId)
                .set(SiteContent::getStatus, status));
    }

    @Override
    public void updateMediaStatus(Long contentMediaId, String localUrl, String status, String type) {
        this.mediaMapper.update(Wrappers.<SiteContentMedia>lambdaUpdate()
                .eq(SiteContentMedia::getId, contentMediaId)
                .set(SiteContentMedia::getStatus, status)
                .set(type != null, SiteContentMedia::getType, type)
                .set(SiteContentMedia::getLocalUrl, localUrl));
    }

    @Override
    public List<SiteContentMedia> listMediasByContentId(Long siteContentId) {
        return this.mediaMapper.selectList(Wrappers
                .<SiteContentMedia>lambdaQuery()
                .eq(SiteContentMedia::getSiteContentId, siteContentId));
    }

    @Override
    public Result<SiteContent> detailById(Long id) {
        List<SiteContentMedia> siteContentMedia = this.mediaMapper
                .selectList(Wrappers.<SiteContentMedia>lambdaQuery()
                        .eq(SiteContentMedia::getSiteContentId, id)
                        .isNotNull(SiteContentMedia::getLocalUrl));

        return this.getById(id).ifPresent(item -> item.setMedias(siteContentMedia));
    }

    @Override
    public Long countMedia(String contentId) {
        SiteContent data = this.getByPromotionId(contentId).getData();
        if(data != null) {
            return mediaMapper.selectCount(Wrappers.<SiteContentMedia>lambdaQuery()
                    .eq(SiteContentMedia::getSiteContentId, data.getId())
                    .isNotNull(SiteContentMedia::getLocalUrl));
        }

        return 0L;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeContentAndMedia(String contentId) {
        this.getByPromotionId(contentId).ifPresent(item -> {
            this.removeById(item.getId());
            mediaMapper.delete(Wrappers.<SiteContentMedia>lambdaUpdate().eq(SiteContentMedia::getSiteContentId, item.getId()));
        });
    }

    @Override
    public void removeMedia(Long mediaId) {
        mediaMapper.deleteById(mediaId);
    }

    @Override
    public void removeMediaByLocalUrlIsNull(String contentId) {
        this.getByPromotionId(contentId).ifPresent(item -> {
           this.mediaMapper.delete(Wrappers.<SiteContentMedia>lambdaUpdate()
                   .eq(SiteContentMedia::getSiteContentId, item.getId())
                   .isNull(SiteContentMedia::getLocalUrl));
        });
    }
}
